package com.huixin.zhang.common.bean.code;

import com.huixin.zhang.common.enums.ResultCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.util.StringUtils;


@Data
@AllArgsConstructor
public class TypeResultBean<T> {
	private int code;
	private String msg;
	private T data;

	public boolean isSuccess(){
		return code == ResultCode.SUCCESS.getCode();
	}
	
	public static <T> TypeResultBean<T> success() {
		ResultCode resultCode = ResultCode.SUCCESS;
		TypeResultBean<T> result = new TypeResultBean<T>(resultCode.getCode(), resultCode.getMsg(), null);
		return result;
	}
	
	public static <T> TypeResultBean<T> success(T data) {
		ResultCode resultCode = ResultCode.SUCCESS;
		TypeResultBean<T> result = new TypeResultBean<T>(resultCode.getCode(), resultCode.getMsg(), data);
		return result;
	}

	/**
	 * 登录超时
	 * @return
	 */
	public static <T>TypeResultBean<T> timeout(){
		return failure(ResultCode.ERROR_100);
	}

	/**
	 * 数据不存在
	 * @return
	 */
	public static <T> TypeResultBean<T> notfund(){
		return failure(ResultCode.ERROR_103);
	}
	
	public static <T> TypeResultBean<T> failure(int code, String msg) {
		TypeResultBean<T> result = new TypeResultBean<T>(code, msg, null);
		return result;
	}
	
	public static <T> TypeResultBean<T> failure(ResultCode resultCode) {
		TypeResultBean<T> result = new TypeResultBean<T>(resultCode.getCode(), resultCode.getMsg(), null);
		return result;
	}
	
	public static <T> TypeResultBean<T> failure(ResultCode resultCode, String msg) {
		if(StringUtils.isEmpty(msg)) {
			msg = resultCode.getMsg();
		}
	  TypeResultBean<T> result = new TypeResultBean<T>(resultCode.getCode(), msg, null);
		return result;
	}
	
	public ResultBean convert() {
		return new ResultBean(this.code, this.msg, this.data);
	}
}
